<template>
  <div class="crowdfunding-list">
    <el-card v-for="item in list" :key="item.id" class="crowdfunding-item">
      <div class="item-header">
        <div class="project-info">
          <el-image :src="item.image" class="project-image" />
          <div class="project-detail">
            <h3>{{ item.title }}</h3>
            <div class="tags">
              <el-tag 
                size="small" 
                :type="getProgressTagType(item.progress)"
              >
                {{ getProgressText(item.progress) }}
              </el-tag>
            </div>
          </div>
        </div>
        <div class="order-info">
          <p>订单号：{{ item.orderNo }}</p>
          <p>参与时间：{{ item.joinTime }}</p>
        </div>
      </div>

      <div class="item-content">
        <div class="progress-info">
          <el-progress 
            :percentage="item.progressPercentage" 
            :status="getProgressStatus(item.progress)"
          />
          <div class="progress-detail">
            <div class="progress-item">
              <div class="label">目标数量</div>
              <div class="value">{{ item.targetQuantity }}件</div>
            </div>
            <div class="progress-item">
              <div class="label">已筹数量</div>
              <div class="value">{{ item.currentQuantity }}件</div>
            </div>
            <div class="progress-item">
              <div class="label">我的认购</div>
              <div class="value highlight">{{ item.myQuantity }}件</div>
            </div>
            <div class="progress-item">
              <div class="label">剩余时间</div>
              <div class="value">{{ item.remainingDays }}天</div>
            </div>
          </div>
        </div>

        <div class="item-summary">
          <div class="price-info">
            <div class="price">
              <span class="label">众筹价：</span>
              <span class="amount">¥{{ item.price }}</span>
            </div>
            <div class="total">
              <span class="label">支付金额：</span>
              <span class="amount">¥{{ item.amount }}</span>
            </div>
          </div>
          
          <div class="actions">
            <el-button 
              v-if="status === 'ongoing'"
              type="danger" 
              @click="$emit('quit', item)"
            >
              退出众筹
            </el-button>
            <el-button 
              type="primary"
              @click="viewDetail(item)"
            >
              查看详情
            </el-button>
          </div>
        </div>
      </div>

      <!-- 规格信息 -->
      <div class="specs-info">
        <div class="specs-title">已选规格：</div>
        <div class="specs-list">
          <div v-for="spec in item.specs" :key="spec.id" class="spec-item">
            <span>颜色：{{ spec.color }} 尺寸：{{ spec.size }}</span>
            <span class="quantity">x{{ spec.quantity }}</span>
          </div>
        </div>
      </div>
    </el-card>

    <!-- 空状态 -->
    <el-empty
      v-if="list.length === 0"
      description="暂无相关众筹记录"
    />
  </div>
</template>

<script setup>
import { defineProps, defineEmits } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

const props = defineProps({
  list: {
    type: Array,
    default: () => []
  },
  status: {
    type: String,
    required: true
  }
})

defineEmits(['quit'])

// 进度状态处理
const getProgressTagType = (progress) => {
  const typeMap = {
    'funding': 'warning',    // 募集中
    'success': 'success',    // 募集成功
    'producing': 'primary',  // 生产中
    'shipping': 'info',      // 配送中
    'failed': 'danger'       // 募集失败
  }
  return typeMap[progress]
}

const getProgressText = (progress) => {
  const textMap = {
    'funding': '募集中',
    'success': '募集成功',
    'producing': '生产中',
    'shipping': '配送中',
    'failed': '募集失败'
  }
  return textMap[progress]
}

const getProgressStatus = (progress) => {
  if (progress === 'failed') return 'exception'
  if (progress === 'success' || progress === 'shipping') return 'success'
  return ''
}

// 查看详情
const viewDetail = (item) => {
  router.push(`/crowdfunding/detail/${item.id}`)
}
</script>

<style lang='scss' scoped>
.crowdfunding-list {
  .crowdfunding-item {
    margin-bottom: 20px;
  }
}

.item-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.project-info {
  display: flex;
  align-items: flex-start;

  .project-image {
    width: 120px;
    height: 120px;
    margin-right: 15px;
    border-radius: 4px;
  }

  .project-detail {
    h3 {
      margin: 0 0 10px 0;
    }

    .tags {
      .el-tag + .el-tag {
        margin-left: 10px;
      }
    }
  }
}

.order-info {
  text-align: right;
  color: #666;
  font-size: 14px;

  p {
    margin: 5px 0;
  }
}

.progress-info {
  margin: 20px 0;

  .progress-detail {
    display: flex;
    justify-content: space-around;
    margin-top: 15px;
    text-align: center;
  }

  .progress-item {
    .label {
      color: #666;
      font-size: 12px;
      margin-bottom: 5px;
    }

    .value {
      font-size: 16px;
      font-weight: bold;

      &.highlight {
        color: #409EFF;
      }
    }
  }
}

.item-summary {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 0;
  border-top: 1px solid #eee;
}

.price-info {
  .price, .total {
    margin: 5px 0;
  }

  .label {
    color: #666;
  }

  .amount {
    color: #ff4400;
    font-size: 16px;
    font-weight: bold;
  }
}

.specs-info {
  margin-top: 15px;
  padding-top: 15px;
  border-top: 1px solid #eee;
  font-size: 14px;

  .specs-title {
    color: #666;
    margin-bottom: 10px;
  }

  .specs-list {
    display: flex;
    flex-wrap: wrap;
  }

  .spec-item {
    margin-right: 20px;
    margin-bottom: 5px;

    .quantity {
      color: #999;
      margin-left: 5px;
    }
  }
}
</style>